{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.020000000000000018\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "rate_return = 102.0/100 - 1\n",
    "print(rate_return)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "               Open   Close  log_price  log_return\n",
      "Date                                              \n",
      "2017-03-01  137.890  139.79   4.940141         NaN\n",
      "2017-03-02  140.000  138.96   4.934186   -0.005955\n",
      "2017-03-03  138.780  139.78   4.940070    0.005884\n",
      "2017-03-06  139.365  139.34   4.936917   -0.003153\n",
      "2017-03-07  139.060  139.52   4.938208    0.001291\n",
      "2017-03-08  138.950  139.00   4.934474   -0.003734\n",
      "2017-03-09  138.740  138.68   4.932169   -0.002305\n",
      "2017-03-10  139.250  139.14   4.935481    0.003311\n",
      "2017-03-13  138.850  139.20   4.935912    0.000431\n",
      "2017-03-14  139.300  138.99   4.934402   -0.001510\n",
      "2017-03-15  139.410  140.46   4.944923    0.010521\n",
      "2017-03-16  140.720  140.69   4.946559    0.001636\n",
      "2017-03-17  141.000  139.99   4.941571   -0.004988\n",
      "2017-03-20  140.400  141.46   4.952017    0.010446\n",
      "2017-03-21  142.110  139.84   4.940499   -0.011518\n",
      "2017-03-22  139.845  141.42   4.951734    0.011235\n",
      "2017-03-23  141.260  140.92   4.948192   -0.003542\n",
      "2017-03-24  141.500  140.64   4.946203   -0.001989\n",
      "2017-03-27  139.390  140.88   4.947908    0.001705\n",
      "2017-03-28  140.910  143.80   4.968423    0.020515\n",
      "2017-03-29  143.680  144.12   4.970646    0.002223\n",
      "2017-03-30  144.190  143.93   4.969327   -0.001319\n",
      "2017-03-31  143.720  143.66   4.967449   -0.001878\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import quandl\n",
    "quandl.ApiConfig.api_key = 'tAyfv1zpWnyhmDsp91yv'\n",
    "#get quandl data\n",
    "aapl_table = quandl.get('WIKI/AAPL')\n",
    "aapl = aapl_table.loc['2017-3',['Open','Close']]\n",
    "#take log return\n",
    "aapl['log_price'] = np.log(aapl.Close)\n",
    "aapl['log_return'] = aapl.log_price.diff()\n",
    "print(aapl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0273081001636184\n"
     ]
    }
   ],
   "source": [
    "month_return = aapl.log_return.sum()\n",
    "print(month_return)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.94597446550658\n"
     ]
    }
   ],
   "source": [
    "print(np.mean(aapl.log_price))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.00014203280448152512\n"
     ]
    }
   ],
   "source": [
    "print(np.var(aapl.log_price))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
